package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.enums.ordercenter.OrderCategoryEnum;
import com.epoch.app.crforecastsales.bo.ForecastOrderBO;
import com.epoch.app.crforecastsales.bo.ForecastOrderLineBO;
import com.epoch.app.crpricecenter.model.enums.PriceTypeEnum;
import com.epoch.app.mnordercenter.model.dto.PolicyPriceMainRequest;
import com.epoch.app.mnordercenter.model.dto.PolicyPriceQueryRequest;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author yuanwei
 * @date 2023/11/09
 */
public class ForecastOrderBOToPriceOrderInfoConvertor {

    public static PolicyPriceMainRequest convert(ForecastOrderBO forecastOrderBO) {
        PolicyPriceMainRequest mainOrderInfo = new PolicyPriceMainRequest();
        mainOrderInfo.setBusinessType(forecastOrderBO.getBusinessType());
        mainOrderInfo.setFromSys(forecastOrderBO.getFromSys());
        mainOrderInfo.setSalesOrganizationCode(forecastOrderBO.getSalesOrganizationCode());
        mainOrderInfo.setCalculateItemList(convertOrderLine(forecastOrderBO.getForecastOrderLineBOList(), forecastOrderBO));
        return mainOrderInfo;
    }

    private static List<PolicyPriceQueryRequest> convertOrderLine(List<ForecastOrderLineBO> orderLines, ForecastOrderBO forecastOrderBO) {
        if (CollectionUtils.isEmpty(orderLines)) {
            return new ArrayList<>();
        }
        List<PolicyPriceQueryRequest> responses = new ArrayList<>();
        for (ForecastOrderLineBO orderLine : orderLines) {
            if (Objects.nonNull(orderLine.getUnitPrice())) {
                // 传入单价时，以入参为准，不用去价格中心查询价格
                continue;
            }
            responses.add(lineBOToSDO(orderLine, forecastOrderBO));
        }
        return responses;
    }

    public static PolicyPriceQueryRequest lineBOToSDO(ForecastOrderLineBO orderLine, ForecastOrderBO forecastOrder) {
        String priceTypeCode = PriceTypeEnum.ZP01.getCode(); // 默认查询常规价
        if (OrderCategoryEnum.GROUP.getCode().equals(forecastOrder.getOrderCategoryCode())) {
            priceTypeCode = PriceTypeEnum.ZMN1.getCode(); // 集团预报订单查询到岸价
        }

        PolicyPriceQueryRequest response = PolicyPriceQueryRequest.builder()
                .priceTypeCode(priceTypeCode)
                .orgSalesOrganizationCode(forecastOrder.getSalesOrganizationCode())
                .channelCode(forecastOrder.getChannelCode())
                .scItemGroupCode(forecastOrder.getProductGroupCode())
                .customerCode(forecastOrder.getSoldToPartyCode())
                .customerSaleAddressCode(forecastOrder.getSoldToPartyCode())
                .customerSendAddressCode(forecastOrder.getShipToPartyCode())
                .salesUnitCode(orderLine.getSaleUnitCode())
                .materialCode(orderLine.getProductCode())
                .departmentCode(forecastOrder.getSalesDepartmentCode())
                .factoryCode(orderLine.getDeliveryFactoryCode())
                .storePlace(orderLine.getStore())
                .unitPrice(orderLine.getUnitPrice())
                .build();
        return response;
    }
}
